package com.ruoyi.fantasy.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.huanzhidadi.fantasy.common.constant.RedisConstants;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.fantasy.domain.FantasyItemType;
import com.ruoyi.fantasy.domain.FantasyTaskCompletionConditionBO;
import com.ruoyi.fantasy.mapper.FantasyItemtypeMapper;
import com.ruoyi.fantasy.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.fantasy.mapper.FantasyTaskCompletionConditionMapper;
import com.huanzhidadi.fantasy.bean.domain.FantasyTaskCompletionCondition;
import com.ruoyi.fantasy.service.IFantasyTaskCompletionConditionService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 任务完成条件关系 服务层实现
 * 
 * @author wanghui
 * @date 2019-07-05
 */
@Service
public class FantasyTaskCompletionConditionServiceImpl implements IFantasyTaskCompletionConditionService 
{
	@Autowired
	private FantasyTaskCompletionConditionMapper fantasyTaskCompletionConditionMapper;

	@Resource
	private FantasyItemtypeMapper fantasyItemtypeMapper;

	@Autowired
	private RedisUtil redisUtil;

	/**
     * 查询任务完成条件关系信息
     * 
     * @param id 任务完成条件关系ID
     * @return 任务完成条件关系信息
     */
    @Override
	public FantasyTaskCompletionCondition selectFantasyTaskCompletionConditionById(String id)
	{
	    return fantasyTaskCompletionConditionMapper.selectFantasyTaskCompletionConditionById(id);
	}
	
	/**
     * 查询任务完成条件关系列表
     * 
     * @param fantasyTaskCompletionCondition 任务完成条件关系信息
     * @return 任务完成条件关系集合
     */
	@Override
	public List<FantasyTaskCompletionCondition> selectFantasyTaskCompletionConditionList(FantasyTaskCompletionCondition fantasyTaskCompletionCondition)
	{
	    return fantasyTaskCompletionConditionMapper.selectFantasyTaskCompletionConditionList(fantasyTaskCompletionCondition);
	}
	
    /**
     * 新增任务完成条件关系
     * 
     * @param fantasyTaskCompletionCondition 任务完成条件关系信息
     * @return 结果
     */
	@Override
	public int insertFantasyTaskCompletionCondition(FantasyTaskCompletionCondition fantasyTaskCompletionCondition)
	{
	    return fantasyTaskCompletionConditionMapper.insertFantasyTaskCompletionCondition(fantasyTaskCompletionCondition);
	}
	
	/**
     * 修改任务完成条件关系
     * 
     * @param fantasyTaskCompletionCondition 任务完成条件关系信息
     * @return 结果
     */
	@Override
	public int updateFantasyTaskCompletionCondition(FantasyTaskCompletionCondition fantasyTaskCompletionCondition)
	{
	    return fantasyTaskCompletionConditionMapper.updateFantasyTaskCompletionCondition(fantasyTaskCompletionCondition);
	}

	/**
     * 删除任务完成条件关系对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteFantasyTaskCompletionConditionByIds(String ids)
	{
		return fantasyTaskCompletionConditionMapper.deleteFantasyTaskCompletionConditionByIds(Convert.toStrArray(ids));
	}

	@Override
	public List<FantasyTaskCompletionConditionBO> selectAllocatedList(FantasyTaskCompletionConditionBO fantasyTaskCompletionConditionBO) {
		return fantasyTaskCompletionConditionMapper.selectAllocatedList(fantasyTaskCompletionConditionBO);
	}

	@Override
	public int deleteFantasyTaskCompletionCondition(FantasyTaskCompletionConditionBO fantasyTaskCompletionConditionBO) {
		return fantasyTaskCompletionConditionMapper.deleteFantasyTaskCompletionCondition(fantasyTaskCompletionConditionBO);
	}

	@Override
	public int deleteAllocCompletionConditions(String taskId, String completionIds) {
		String[] completionIdArray = completionIds.split(",");
		return fantasyTaskCompletionConditionMapper.deleteAllocCompletionConditions(taskId, completionIdArray);
	}

	@Override
	public int insertAllocCompletionConditions(String taskId, String completionIds) {
		String[] completionIdArray = Convert.toStrArray(completionIds);
		// 新增任务完成条件对应关系
		List<FantasyTaskCompletionCondition> list = new ArrayList<>();
		for (String completionId : completionIdArray)
		{
			FantasyTaskCompletionCondition fantasyTaskCompletionCondition = new FantasyTaskCompletionCondition();
			fantasyTaskCompletionCondition.setId(String.valueOf(IdWorker.nextId()));
			fantasyTaskCompletionCondition.setTaskId(taskId);
			fantasyTaskCompletionCondition.setCompletionId(completionId);
			list.add(fantasyTaskCompletionCondition);
		}
		return fantasyTaskCompletionConditionMapper.insertAllocCompletionConditions(list);
	}

	@Override
	public void transforData(List<FantasyTaskCompletionConditionBO> list) {
		Map<String, String> itemTypes = redisUtil.hmGet(RedisConstants.FANTASY_ITEM_TYPE);
		for (FantasyTaskCompletionConditionBO fantasyTaskCompletionConditionBO : list) {
			if (fantasyTaskCompletionConditionBO.getType() == 3 || fantasyTaskCompletionConditionBO.getType() == 4) {
				FantasyItemType fantasyItemType = JSONObject.parseObject(itemTypes.get(fantasyTaskCompletionConditionBO.getValue()), FantasyItemType.class);
				if (null == fantasyItemType) {
					fantasyItemType = fantasyItemtypeMapper.selectFantasyItemtypeById(fantasyTaskCompletionConditionBO.getValue());
				}
				fantasyTaskCompletionConditionBO.setValue(fantasyItemType.getName());
			}
		}
	}

}
